/**   
 * @Title: MainActivityHealthNewsFragmentBodyFragment.java 
 * @Package cn.edu.scu.healthclient.activity.fragment 
 * @Description: TODO 
 * @author 
 * @date 
 * @version V0.0   
 */
package cn.edu.cdtu.healthclient.activity.fragment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import cn.edu.cdtu.healthclient.activity.MainActivity;
import cn.edu.cdtu.healthclient.activity.NewsDetailActivity;
import cn.edu.cdtu.healthclient.connect.ConnectForHealthNews;
import cn.edu.cdtu.healthclient.domain.HealthNewsJson;
import cn.edu.scu.healthclient.activity.R;

/** 
 * @ClassName: MainActivityHealthNewsFragmentBodyFragment 
 * @Description: TODO
 * @author 
 * @date 
 *  
 */
public class MainActivityHealthNewsFragmentBodyFragment extends Fragment {

	private View mainView;
	private ListView bodyNewsListView;
	private int imageIds[]=new int[]
			{
			R.drawable.body1,R.drawable.body2,R.drawable.body3,
			R.drawable.body4,R.drawable.body5,R.drawable.body6,
			R.drawable.body7,R.drawable.body8,R.drawable.body9,
			R.drawable.body10
			};
	private String titles[]=new String[]
			{
			"癌症有八面免死金牌",
			"经常掏耳朵会致癌",
			"子宫须保护",
			"你很少吃的6种好食物",
			"坚持做10件事有望活过85岁",
			"从声音中分辨脏腑问题",
			"哪些人不宜吃花生",
			"如何让元宵吃得健康",
			"健康饮食不是吃糠咽菜",
			"春季吃韭菜的四大好处"
			};
	private String describes[]=new String[]
			{
			"癌症是非常可怕的，但是如果注意生活方式，可以降低癌症的发病几率。",
			"有的人耳朵痒了，常常挖耳勺、用发卡、火柴棍儿等掏耳朵，其实这样做有很多害处。",
			"全国政协委员、重庆医科大学妇产科教授王智彪建议设立“世界子宫日”。",
			"人的探险精神如果用在饮食上，一定能获得更多的健康。",
			"至少做到两点：一是戒烟，二是涂抹防晒霜。每天太阳直晒别超过15分钟。",
			"肝气过盛或肝阳上亢的人，常常容易发怒，大呼小叫。",
			"花生又叫落花生，因其能滋养补益，具有养生延年的功效，在民间被叫做长生果。",
			"今天是传统佳节元宵节，怎样煮元宵其实也有窍门，如何吃得健康教你几招。",
			"油炸食品是不是彻底不能吃？方便面一类油盐高的食品是不是要躲远点？",
			"在初春的此时适当吃一些韭菜会有很多好处的，因为韭菜的营养价值对我们的身体健康来说有很多帮助。"
			};
	/* (non-Javadoc)
	 * <p>Title: onCreate</p> 
	 * <p>Description: </p> 
	 * @param savedInstanceState 
	 * @see android.support.v4.app.Fragment#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		LayoutInflater inflater = getActivity().getLayoutInflater();
		mainView = inflater.inflate(
				R.layout.activity_main_healthnews_fragment_body_fragment,
				(ViewGroup)getActivity().findViewById(R.id.viewPager), false);

		bodyNewsListView=(ListView)mainView.findViewById(R.id.BodyNewsListView);
		List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();
		for(int i=0;i<imageIds.length;i++)
		{
			Map<String,Object> listItem=new HashMap<String,Object>();
			listItem.put("image",imageIds[i]);
			listItem.put("title",titles[i]);
			listItem.put("describe", describes[i]);
			listItems.add(listItem);
		}
		SimpleAdapter simpleAdapter=new SimpleAdapter(getActivity(),
				listItems,
				R.layout.activity_main_healthnews_fragment_body_fragment_bodystyle,
				new String[]{"image","describe","title"},
				new int[]{R.id.BodyNewsImage,R.id.BodyNewsDescribe,R.id.BodyNewsTitle}
				);
		bodyNewsListView.setAdapter(simpleAdapter);
		bodyNewsListView.setOnItemClickListener
		(
				new OnItemClickListener()
				{
					public void onItemClick(AdapterView<?> parent,
							View view,int position,long id)
					{
						final HealthNewsAsync healthNewsAsync=new HealthNewsAsync();
						Log.i("ali","postion="+position);
						final String idString=Integer.toString(position+1);
						((MainActivity)getActivity()).getTaskQueue().addTask(
								new Thread(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								healthNewsAsync.execute(idString,"body");
							}
						}));
					}
				}
				);
	}
	/* (non-Javadoc)
	 * <p>Title: onCreateView</p> 
	 * <p>Description: </p> 
	 * @param inflater
	 * @param container
	 * @param savedInstanceState
	 * @return 
	 * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		ViewGroup viewGroup = (ViewGroup) mainView.getParent(); 
		if (viewGroup!= null) 
		{ 
			viewGroup .removeAllViewsInLayout(); 
			Log.v("ali", "移除已存在的View");
		} 
		return mainView;
	}
	/**
	 * 
	 * @ClassName: HealthNewsAsync 
	 * @Description: TODO
	 * @author 
	 * @date 
	 *
	 */
	private class HealthNewsAsync extends AsyncTask<String,Void,HealthNewsJson>
	{

		protected HealthNewsJson doInBackground(String ... params)
		{
			HealthNewsJson healthNewsJson = null;
			try 
			{
				healthNewsJson=ConnectForHealthNews.connect(params[0],params[1],
						((MainActivity)getActivity()).getBinder().getHttpClient());
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
			return healthNewsJson;
		}
		protected void onPostExecute(HealthNewsJson healthNewsJson)
		{
			Bundle bundle=new Bundle();
			bundle.putSerializable("healthNewsJson",healthNewsJson);
			Intent newsDetailIntent=new Intent(getActivity(),NewsDetailActivity.class);
			newsDetailIntent.putExtras(bundle);
			startActivity(newsDetailIntent);
		}
	}
}
